<?php (defined('BASEPATH')) OR exit('No direct script access allowed');


class Setting extends CI_Controller 
{

	function __construct()
	{
		parent::__construct();	
		$this->template->set_template('template/admin_template');
		if($this->general_model->loggedin("admin") == FALSE)
		{
			redirect('admin/users/login');
		}
	}
	
	function index()
	{
        $data['breadcrumb'] = set_crumbs(array(current_url() => 'General Settings'));
		//$data['pages'] = $this->general_model->select_rows('pages','*','title','asc','10');
		$data['pages'] = $this->general_model->select("pages","pageid,title","status = 'published'");
		$data['settings']=$this->general_model->select_val("setting","*","id = 1");
		
		if($_POST)
		{
			$settingdata=array("site_name"=>$this->input->post('site_name'),
								"notification_email"=>$this->input->post('notification_email'),
								
								"site_homepage"=>$this->input->post('site_homepage'),
								"facebook"=>$this->input->post('facebook'),
								"twitter"=>$this->input->post('twitter'),
								"youtube"=>$this->input->post('youtube'),
								"paypal_email"=>$this->input->post('paypal_email'),
								"sandbox_mode"=>$this->input->post('sandbox_mode'),
								"business_price"=>$this->input->post('business_price'),
								"site_logo"=>$this->input->post('site_logo'),
								);
			$this->general_model->update("setting",$settingdata,"id = 1");
			$updatedata=array("is_home"=>1);
			$noupdatedata=array("is_home"=>0);
			$this->general_model->update("pages",$updatedata,"pageid =".$this->input->post('site_homepage'));
			$this->general_model->update("pages",$noupdatedata,"pageid !=".$this->input->post('site_homepage'));
			$this->session->set_flashdata('message', '<p class="success">Setting added Saved.</p>');
			
			redirect(ADMIN_PATH . "/setting");
			
		}
		$this->template->content->view('admin/system/general_setting', $data);
		$this->template->publish();
    }
	public function countries()
	{
		$data['breadcrumb'] = set_crumbs(array(current_url() => 'Countires'));
		$data['totalcountries']=$this->general_model->select("country");
		$count=count($data['totalcountries']);
		$perpage = 10;
		if ($count > $perpage) {
			$this->load->library('pagination');
		    $data['query_string'] = ( ! empty($_SERVER['QUERY_STRING'])) ? '?' . $_SERVER['QUERY_STRING'] : '';

			
			
			// Create Pagination
			$config['base_url'] = site_url(ADMIN_PATH . '/setting/countries/');
			$config['total_rows'] = $count;
			$config['per_page'] = $perpage; 
			$config['uri_segment'] = '4';
			$config['num_links'] = 4 ;
			$config['full_tag_open'] = false;
			$config['full_tag_close'] = false;
			$config['cur_tag_open'] = '<a href="#">';
			$config['cur_tag_close'] = '</a>';
			$config['next_tag_open'] = false;
			$config['next_tag_close'] = false;
			$config['prev_tag_open'] = false;
			$config['prev_tag_close'] = false;
			
			$config['num_tag_open'] = false;
			$config['num_tag_close'] = false;
			$config['suffix'] = $data['query_string'];
			$this->pagination->initialize($config); 
			$data['pagination'] = $this->pagination->create_links();
			$offset = $this->uri->segment(4);
			
			
			
			
		}
		$data['countries'] = $this->general_model->select_rows("country","*","country_id","asc",$perpage,$offset);
		$this->template->content->view('admin/system/countries', $data);
		$this->template->publish();
	}
	public function addcountry()
	{
		$data['breadcrumb'] = set_crumbs(array('setting/countries' => 'All Country', current_url() => 'Add Country'));
		$this->form_validation->set_rules('name', 'Country Name', 'required');
        $this->form_validation->set_rules('iso_code_3', 'ISO Code', 'trim|required');
			
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				
				$countrydata=array(
					"name"=>$this->input->post('name'),
					
					"iso_code_3"=>$this->input->post('iso_code_3'),
					"status"=>$this->input->post('status'),
					
				);
				$countryid=$this->general_model->insert('country',$countrydata);
				
				$this->session->set_flashdata('message', '<p class="success">country added successfully</p>');
				
				redirect(ADMIN_PATH . '/setting/countries');
			}
		$this->template->content->view('admin/system/addcountry', $data);
		$this->template->publish();
	}
	public function editcountry()
	{
		$data['breadcrumb'] = set_crumbs(array('setting/countries' => 'All Countries', current_url() => 'Edit Country'));
		$countryid=$this->uri->segment(4);
		$this->form_validation->set_rules('name', 'Country Name', 'required');
        $this->form_validation->set_rules('iso_code_3', 'ISO Code', 'trim|required');
			
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				
				$countrydata=array(
					"name"=>$this->input->post('name'),
					
					"iso_code_3"=>$this->input->post('iso_code_3'),
					"status"=>$this->input->post('status'),
					
				);
				$this->general_model->update('country',$countrydata,"country_id = ".$countryid);
				
				$this->session->set_flashdata('message', '<p class="success">Practice updated successfully</p>');
				
				redirect(ADMIN_PATH . '/setting/countries');
			}
		
		$data['country']=$this->general_model->select_val("country","*","country_id = ".$countryid);
		$this->template->content->view('admin/system/editcountry',$data);
		$this->template->publish();
	}
	
	public function states()
	{
		$data['breadcrumb'] = set_crumbs(array(current_url() => 'States'));
		$data['totalstates']=$this->general_model->select("zone","*","country_id ='223'");
		$count=count($data['totalstates']);
		$perpage = 10;
		if ($count > $perpage) {
			$this->load->library('pagination');
		    $data['query_string'] = ( ! empty($_SERVER['QUERY_STRING'])) ? '?' . $_SERVER['QUERY_STRING'] : '';

			
			
			// Create Pagination
			$config['base_url'] = site_url(ADMIN_PATH . '/setting/states/');
			$config['total_rows'] = $count;
			$config['per_page'] = $perpage; 
			$config['uri_segment'] = '4';
			$config['num_links'] = 4 ;
			$config['full_tag_open'] = false;
			$config['full_tag_close'] = false;
			$config['cur_tag_open'] = '<a href="#">';
			$config['cur_tag_close'] = '</a>';
			$config['next_tag_open'] = false;
			$config['next_tag_close'] = false;
			$config['prev_tag_open'] = false;
			$config['prev_tag_close'] = false;
			
			$config['num_tag_open'] = false;
			$config['num_tag_close'] = false;
			$config['suffix'] = $data['query_string'];
			$this->pagination->initialize($config); 
			$data['pagination'] = $this->pagination->create_links();
			$offset = $this->uri->segment(4);
			
			
			
			
		}
		
		$states = $this->general_model->select_rows("zone","*","zone_id","asc",$perpage,$offset,"country_id = 223");
		foreach($states as $state)
		{
			$country=$this->general_model->select_val('country','*','country_id ='.$state['country_id']);
			$data['states'][]=array(
							'stateid'=>$state['zone_id'],
							'country_id'=>$state['country_id'],
							'country_name'=>$country['name'],
							'state_name'=>$state['name'],
							'code'=>$state['code'],
							'status'=>$state['status']
							);
		}
		
		$this->template->content->view('admin/system/states', $data);
		$this->template->publish();
	}
	public function addstate()
	{
		$data['breadcrumb'] = set_crumbs(array('setting/states' => 'All States', current_url() => 'Add State'));
		$this->form_validation->set_rules('name', 'State Name', 'required');
        $this->form_validation->set_rules('code', 'State Code', 'trim|required');
			
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				
				$statedata=array(
					"name"=>$this->input->post('name'),
					"country_id"=>$this->input->post('country_id'),
					"code"=>$this->input->post('code'),
					"status"=>$this->input->post('status'),
					
				);
				$countryid=$this->general_model->insert('zone',$statedata);
				
				$this->session->set_flashdata('message', '<p class="success">State added successfully</p>');
				
				redirect(ADMIN_PATH . '/setting/states');
			}
		$data['countries']=$this->general_model->select('country',"*","country_id = 223");
		$this->template->content->view('admin/system/addstate', $data);
		$this->template->publish();
	}
	public function editstate()
	{
		$data['breadcrumb'] = set_crumbs(array('setting/states' => 'All states', current_url() => 'Edit State'));
		$stateid=$this->uri->segment(4);
		$this->form_validation->set_rules('name', 'State Name', 'required');
        $this->form_validation->set_rules('code', 'State Code', 'trim|required');
			
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				
				$statedata=array(
					"name"=>$this->input->post('name'),
					"country_id"=>$this->input->post('country_id'),
					"code"=>$this->input->post('code'),
					"status"=>$this->input->post('status'),
					
				);
				$this->general_model->update('zone',$statedata,"zone_id = ".$stateid);
				
				$this->session->set_flashdata('message', '<p class="success">State updated successfully</p>');
				
				redirect(ADMIN_PATH . '/setting/states');
			}
		
		$data['countries']=$this->general_model->select('country',"*","country_id = 223");
		$data['states']=$this->general_model->select_val("zone","*","zone_id = ".$stateid);
		$this->template->content->view('admin/system/editstate',$data);
		$this->template->publish();
	}
	public function server_info()
	{
		$data = array();
        $data['breadcrumb'] = set_crumbs(array(current_url() => 'Server Info'));
        $this->template->stylesheet->add('assets/admin/css/server_info.css');

        ob_start();
        phpinfo();
		
        $pinfo = ob_get_contents();
        ob_end_clean();
        
        $data['pinfo'] = preg_replace( '%^.*<body>(.*)</body>.*$%ms','$1',$pinfo);

        $this->template->content->view('admin/system/server_info', $data);
		$this->template->publish();
	}
	
	
}

